package com.isay.frameworklib.widget.loading;


import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.isay.frameworklib.R;


public class LoadingDialog {

    private Context mContext = null;
    private ProgressDialog mProgressDialog = null;
    private AnimationDrawable mLoadingCircle = null;
    private DialogDismissListener mListener;
    private Handler mHandler = new Handler();

    private static final int DEFAULT_DURATION = 60 * 1000;
    public static final int REASON_USER_CANCEL = 0;
    public static final int REASON_TIMEOUT = 1;
    public static final int REASON_BACK = 2;


    public LoadingDialog(Context context) {
        mContext = context;
    }

    // 弹出进度条对话框
    public void showDialog(CharSequence msg) {
        showDialog(msg, false);
    }

    public void showDialog(CharSequence msg, boolean cancelAble) {
        if (isShowing()) {
            return;
        }
        initDialog(msg, cancelAble);
    }


    private void initDialog(CharSequence msg, final boolean cancelAble) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_loading, null);
        TextView tvLoading = view.findViewById(R.id.loading_text);
        tvLoading.setText(msg);
        mLoadingCircle = (AnimationDrawable) (view.findViewById(R.id.big_circle)).getBackground();

        mProgressDialog = new ProgressDialog(mContext, R.style.CustomProgressDialog);
        mProgressDialog.setCancelable(cancelAble);
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
        mProgressDialog.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (cancelAble) {
                        dismissDialog(REASON_BACK);
                        return true;
                    }
                }
                return false;
            }
        });
        mProgressDialog.setContentView(view);
        view.post(new Runnable() {
            public void run() {
                mLoadingCircle.start();
            }
        });
    }

    public void dismissDialog() {
        dismissDialog(REASON_USER_CANCEL);
    }

    private void dismissDialog(int reason) {
        try {
            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                mHandler.removeCallbacks(mTimeoutRunnable);
                mLoadingCircle.stop();
                mProgressDialog.dismiss();
                mProgressDialog = null;

                if (mListener != null) {
                    mListener.onDismiss(reason);
                }
            }
        } catch (IllegalArgumentException e) {

        }
    }

    private Runnable mTimeoutRunnable = new Runnable() {

        @Override
        public void run() {
            dismissDialog(REASON_TIMEOUT);
        }
    };

    public boolean isShowing() {
        if (mProgressDialog != null && mProgressDialog.isShowing())
            return true;
        return false;
    }

    public interface DialogDismissListener {
        public void onDismiss(int reason);
    }

    public void setListener(DialogDismissListener listener) {
        this.mListener = listener;
    }
}
